package com.gengao.springbootcase.controller;

import com.gengao.springbootcase.pojo.Emp;
import com.gengao.springbootcase.pojo.PageBean;
import com.gengao.springbootcase.pojo.Result;
import com.gengao.springbootcase.service.EmpService;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.jmx.export.naming.IdentityNamingStrategy;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Pattern;

@Slf4j
@RestController
public class EmpController {
//    分页查询员工 controller层是与前端交互的 使用了pageHelper也不要进行修改
    @Autowired
    private EmpService empService;
    @RequestMapping(value = "emps",method = RequestMethod.GET)
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,
                       Short gender,
                       @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询，参数{},{},{},{}",page,pageSize,name,gender);
        PageBean pageBean=empService.page(page,pageSize,name,gender, begin,end);
        return Result.success(pageBean);

        }

    @RequestMapping(value = "/emps/{ids}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除操作，ids{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    @RequestMapping(value = "/emps",method = RequestMethod.DELETE)
    public Result deleteByParamNotPath(@RequestParam List<Integer> ids) {
        log.info("批量删除操作，ids{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    @RequestMapping(value = "/emps",method = RequestMethod.POST)
    public Result add(@RequestBody Emp emp){
        log.info("新增员工，emp:{}",emp);
        empService.save(emp);
        return Result.success();
    }

//    根据id查询员工信息
    @RequestMapping(value = "/emps/{id}",method = RequestMethod.GET)
    public Result getById(@PathVariable Integer id){
        log.info("根据id:{}查询员工信息",id);
        Emp emp=empService.getById(id);
        return Result.success(emp);
    }

//    修改用户数据

    @RequestMapping(value = "/emps",method = RequestMethod.PUT)
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息：{}",emp);
        empService.update(emp);

    return Result.success();
    }
}

